package ru.spb.osll.myfit.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.junit.Test;

public class CollectionUtilsTest {

	private class StringFilter implements Filter<String> {
		public boolean passes(String arg) {
			return arg.endsWith("t");
		}
	}

	@Test
	public void testList() {
		List<String> list = CollectionUtils.list("1");
		assertEquals(1, list.size());
		assertEquals("1", list.get(0));
		list = CollectionUtils.list("3", "4");
		assertEquals(2, list.size());
		assertEquals("3", list.get(0));
		assertEquals("4", list.get(1));

	}

	@Test
	public void testFilter() {
		List<String> list = CollectionUtils.list("fun", "test", "stub", "rest");
		List<String> filteredList = CollectionUtils.filter(list,
				new StringFilter());
		assertEquals(2, filteredList.size());
		assertEquals("test", filteredList.get(0));
		assertEquals("rest", filteredList.get(1));
	}

	@Test
	public void testMap() {
		Map<String, String> map = CollectionUtils.map(CollectionUtils
				.list("key"), CollectionUtils.list("value"));
		assertEquals(1, map.size());
		assertTrue(map.containsKey("key"));
		assertEquals("value", map.get("key"));
		map = CollectionUtils.map(CollectionUtils.list("key1", "key2"),
				CollectionUtils.list("value1", "value2"));
		assertEquals(2, map.size());
		assertTrue(map.containsKey("key1"));
		assertEquals("value1", map.get("key1"));
		assertTrue(map.containsKey("key2"));
		assertEquals("value2", map.get("key2"));
	}

	@Test
	public void testFilterEven() {
		assertEquals("", StringUtils.join(CollectionUtils
				.filterEven(new LinkedList<String>()), ""));
		assertEquals("0", StringUtils.join(CollectionUtils
				.filterEven(CollectionUtils.list("0")), ""));
		assertEquals("0", StringUtils.join(CollectionUtils
				.filterEven(CollectionUtils.list("0", "1")), ""));
		assertEquals("02", StringUtils.join(CollectionUtils
				.filterEven(CollectionUtils.list("0", "1", "2")), ""));
	}

	@Test
	public void testFilterOdd() {
		assertEquals("", StringUtils.join(CollectionUtils
				.filterOdd(new LinkedList<String>()), ""));
		assertEquals("", StringUtils.join(CollectionUtils
				.filterOdd(CollectionUtils.list("0")), ""));
		assertEquals("1", StringUtils.join(CollectionUtils
				.filterOdd(CollectionUtils.list("0", "1")), ""));
		assertEquals("1", StringUtils.join(CollectionUtils
				.filterOdd(CollectionUtils.list("0", "1", "2")), ""));
		assertEquals("13", StringUtils.join(CollectionUtils
				.filterOdd(CollectionUtils.list("0", "1", "2", "3")), ""));
	}
}
